package com.jxc.summer.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttribute;

import com.jxc.summer.dao.QualityMapper;
import com.jxc.summer.service.QualityService;
import com.jxc.summer.util.ReturnT;
import com.jxc.summer.util.UUIDUtil;

@Controller
@RequestMapping("/qualityinfo")
public class QualityController {

	@Autowired
	private QualityService qualityService;
	@Autowired
	private QualityMapper qualityMapper;
	@RequestMapping
	public String index(Model model) {
		return "quality/quality.index";
	}
	
	@RequestMapping("/pageList")
	@ResponseBody
	public Map<String, Object> pageList(@SessionAttribute("accountCompanyCode") String companyCode,@RequestParam(required = false, defaultValue = "0") int start,  
			@RequestParam(required = false, defaultValue = "10") int length,@RequestParam String quality) {
		Map<String,Object> params = new HashMap<String, Object>();
		params.put("tq_qualityname", quality);
		params.put("tq_companycode", companyCode);
		params.put("offset", start);
		params.put("pagesize", length);
		Map<String, Object> result = qualityService.pageList(params);
		return result;
	}
	
	@RequestMapping("/add")
	@ResponseBody
	public ReturnT<String> addColor(@SessionAttribute("accountCompanyCode") String companyCode,@RequestParam String quality) {
		Map<String,Object> params = new HashMap<String, Object>();
		params.put("tq_qualityid",UUIDUtil.get32UUID());
		params.put("tq_qualityname", quality);
		params.put("tq_companycode", companyCode);
		//当前商户下颜色不能重复
		if(qualityMapper.validateQuality(params)>0){
			return new ReturnT<String>(500, "新增质量标准失败,质量标准已存在。");
		}
		int result = qualityService.add(params);
		if(result>0){
			return ReturnT.SUCCESS;
		}else{
			return new ReturnT<String>(500, "新增质量标准失败！");
		}
	}
	
	@RequestMapping("/update")
	@ResponseBody
	public ReturnT<String> update(@RequestParam(required = true) String id,
			@SessionAttribute("accountCompanyCode") String companyCode,@RequestParam String quality){
		Map<String,Object> params = new HashMap<String, Object>();
		params.put("tq_qualityid", id);
		params.put("tq_qualityname", quality);
		params.put("tq_companycode", companyCode);
		if(qualityMapper.validateQuality(params)>0){
			return new ReturnT<String>(500, "更新质量标准失败,质量标准重复。");
		}
		int result = qualityService.update(params);
		if(result>0){
			return ReturnT.SUCCESS;
		}else{
			return new ReturnT<String>(500, "更新质量标准失败！");
		}
	}
	
	@RequestMapping("/delete")
	@ResponseBody
	public ReturnT<String> delete(@RequestParam(required = true) String id){
		Map<String,Object> params = new HashMap<String, Object>();
		params.put("tq_qualityid", id);
		int result = qualityService.delete(params);
		if(result>0){
			return ReturnT.SUCCESS;
		}else{
			return new ReturnT<String>(500, "删除质量标准失败！");
		}
	}
}
